﻿function getArtFormData(parentElement)
{
    var doc = nitobi.xml.createXmlDoc('<ArtForm />');
    var docEl = doc.documentElement;
    addFormElements(parentElement, docEl);
    return docEl.xml;
}

function addFormElements(parentElement, docEl)
{
    if (parentElement.hasChildNodes())
    {
        var children = parentElement.childNodes;
        for (var i = 0; i < children.length; i++)
        {
            var child = children[i];
            if (child.nodeName.toUpperCase() == "INPUT")
            {
                docEl.setAttribute("InputType", child.type.toLowerCase());
                if (child.type.toLowerCase() == "checkbox")
                {
                    if (child.checked)
                        docEl.setAttribute(child.name, "true");
                    else
                        docEl.setAttribute(child.name, "false");

                }
                else if (child.type.toLowerCase() == "radio")
                {
                    if (child.checked)
                        docEl.setAttribute(child.name, child.value);
                }
                else
                    docEl.setAttribute(child.name, child.value);
            }
            else if (child.nodeName.toUpperCase() == "TEXTAREA")
            {
                docEl.setAttribute("InputType", child.nodeName.toLowerCase());
                docEl.setAttribute(child.name, child.value);
            }
            else
                addFormElements(child, docEl);
        }
    }

}

function showArtFormData(parentElement, xml)
{
    var doc = nitobi.xml.createXmlDoc(xml);
    var docEl = doc.documentElement;
    setupFormElements(parentElement, docEl);
}

function setupFormElements(parentElement, docEl)
{
    if (parentElement.hasChildNodes())
    {
        var children = parentElement.childNodes;
        for (var i = 0; i < children.length; i++)
        {
            var child = children[i];
            var attrib = findXmlAttributeFromBaseName(child.name, docEl);
            // if(child.name != null && child.name != "" && docEl.getAttribute(child.name) != null)
            if (attrib != null)
            {
                if (child.nodeName.toUpperCase() == "INPUT")
                {
                    if (child.type.toLowerCase() == "checkbox")
                    {
                        if (attrib.value == "true")
                            child.checked = true;
                        else
                            child.checked = false;
                        //child.checked = docEl.getAttribute(child.name)
                    }
                    else if (child.type.toLowerCase() == "radio")
                    {
                        if (attrib.value == child.value)
                            child.checked = true;
                    }
                    else
                        child.value = attrib.value;
                }
                else if (child.nodeName.toUpperCase() == "TEXTAREA")
                    child.value = attrib.value;
                else
                    setupFormElements(child, docEl);
            }
            else
                setupFormElements(child, docEl);
        }
    }

}

function findXmlAttributeFromBaseName(baseName, docEl) {
    if (baseName != null && baseName.length > 0)
    {
        var attribs = docEl.attributes;
        for (var p = 0; p < attribs.length; p++)
            if (attribs[p].name == baseName || (attribs[p].name.length < baseName.length && baseName.substr(0, attribs[p].name.length) == attribs[p].name))
            return attribs[p];
    }
    return null;
}